package com.itheima.service;

import com.alibaba.druid.sql.visitor.functions.Concat;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private JedisPool jedisPool;


    // 新增功能
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        String queryString = queryPageBean.getQueryString();
        Page<Member> page = memberDao.findPage(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }



    @Override
    public void add(Member member) {
        member.setRegTime(new Date());
        memberDao.add(member);
    }

    @Override
    public void delete(Integer id) {

        memberDao.delete(id);
    }

    @Override
    public void update(Member member) {
        memberDao.update(member);
    }

    @Override
    public Member findById(Integer id) {
        return memberDao.findById(id);
    }

    @Override
    public Map findNumsForYear() {
        LocalDate now = LocalDate.now();
        Map<String, Object> map = new HashMap();
        List<String> months = new ArrayList();
        List<Integer> memberCount = new ArrayList();
        for (int i = 0; i < 12; i++) {
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy.MM");
            String month = now.format(dateTimeFormatter);
            String date = month + ".31";
            Integer num = memberDao.findNumsByEndDate(date);
            months.add(month);
            memberCount.add(num);
            now = now.plusMonths(-1);
        }
        Collections.reverse(months);
        Collections.reverse(memberCount);
        map.put("months", months);
        map.put("memberCount", memberCount);
        return map;
    }

    @Override
    //查询今天 本周 本月 和总会员数
    public Map<String, Integer> findNumByDate() {
        LocalDate localDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd");
        //今天日期
        String today = localDate.format(formatter);
        //本周起始日期
        String thisWeekStartDay = localDate.with(DayOfWeek.MONDAY).format(formatter);
        //本月开始日期
        String thisMonthStartDay = LocalDate.of(localDate.getYear(),localDate.getMonth(),1).format(formatter);
        Integer todayNums = memberDao.findNumsByDate(today,null);
        Integer thisWeekNums = memberDao.findNumsByDate(null,thisWeekStartDay);
        Integer thisMonthNums = memberDao.findNumsByDate(null,thisMonthStartDay);
        Integer allNums = memberDao.findNumsByDate(null,null);
        Map<String,Integer> map = new HashMap<>();
        map.put("todayNewMember",todayNums);
        map.put("thisWeekNewMember",thisWeekNums);
        map.put("thisMonthNewMember",thisMonthNums);
        map.put("totalMember",allNums);
        return map;
    }




    @Override
    public void check(String telephone) {
        Member member = memberDao.findByPhoneNumber(telephone);
        //根据手机号判断是否为会员 如果不是 自动注册
        if (member == null){
            member = new Member();
            member.setRegTime(new Date());
            member.setPhoneNumber(telephone);
            memberDao.add(member);
        }
        //将手机号存入redis中 保存时长30分
        Jedis jedis = jedisPool.getResource();
        String json = JSON.toJSON(member).toString();
        jedis.setex(telephone,60*30,json);
    }
}
